package cn.xz.kjg.util;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.util.Log;
import java.io.IOException;
import java.util.regex.Pattern;

import cn.xz.kjg.bean.U3dListBean;
import cn.xz.kjg.net.OkHttpUtil;
import cn.xz.kjg.net.OkhttpResponseHandle;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 程序接口
 */
public class U3dHttp {

    //程序端口号-默认 8030，后根据需求修改
    private static final int mPort = 8030;

    //播放
    public static void PlayMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=play&object=";
        OkHttpUtil.post(url);
    }

    //暂停
    public static void PauseMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=pause&object=";
        OkHttpUtil.post(url);
    }

    //重播
    public static void ReplayMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=replay&object=";
        OkHttpUtil.post(url);
    }

    //最大化/显示
    public static void MaxMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=max&object=";
        OkHttpUtil.post(url);
    }

    //最小化/隐藏
    public static void MinMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=min&object=";
        OkHttpUtil.post(url);
    }

    //上一页
    public static void PrevPage(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=prevPage&object=";
        OkHttpUtil.post(url);
    }

    //下一页
    public static void NextPage(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=nextPage&object=";
        OkHttpUtil.post(url);
    }

    //栏目点播
    public static void PostChannel(String ip, int tag) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=channel&object=" + tag;
        OkHttpUtil.post(url);
    }

    //内容点播
    public static void PostContent(String ip,int tag) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=content&object=" + tag;
        OkHttpUtil.post(url);
    }

    //返回主页
    public static void UnityHome(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=home&object=";
        OkHttpUtil.post(url);
    }

    //返回屏保
    public static void UnityScreen(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=screen&object=";
        OkHttpUtil.post(url);
    }

    //获取列表数据
    public interface GetU3dDataListener{
        void getData(U3dListBean mBean); //有参方法
    }
    public static void getU3dData(Context mContext, String ip, GetU3dDataListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=json&object=";
        OkHttpUtil.get(mContext, url, U3dListBean.class, new OkhttpResponseHandle<U3dListBean>() {
            @Override
            public void onSuccess(U3dListBean response) {
                if (response != null) {
                     mListener.getData(response);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                Log.e(TAG, "程序接口:失败->" + "获取资源列表");
                mListener.getData(null);
            }
        });
    }

    //获取当前视频进度
    public interface GetMovieSizeListener{
        void getData(String maxTxt,String minTxt,int maxSeekBar,int seekProgress); //有参方法
    }
    public static void getMovieSize(String ip, GetMovieSizeListener mListener){
        String movieUrl =  "http://" + ip + ":" + mPort + "/?act=unity&states=progress&object=";
        OkHttpUtil.get(movieUrl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "程序接口:失败->" + "获取视频进度");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseString = response.body().string();
                    //如果请求数据长度小于6直接不处理，服务端只要请求成功就会返回true，
                    // 这里截取前面字符串判断一下，要截取前几个，正常返回数据里也可能会有
                    if (responseString.length() < 6 || responseString.substring(0,5).contains("true")){
                        return;
                    }
                    String[] strs = responseString.split("\\|");
                    if (strs.length == 2) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("str1", strs[0]);
                                Log.e("str1", strs[1]);
                                int a = 0;
                                int b = 0;
                                if (isNumeric(strs[0].substring(0, 1))) {
                                    if (isNumeric(strs[0])) {
                                        a = Integer.parseInt(strs[0]);
                                    } else {
                                        a = Double.valueOf(strs[0]).intValue();
                                    }
                                } else {
                                    if (isNumeric(strs[0])) {
                                        a = Integer.parseInt(strs[0]);
                                    } else {
                                        a = Double.valueOf(strs[0].substring(1, strs[0].length())).intValue();

                                    }

                                }
                                if (isNumeric(strs[1])) {
                                    b = Integer.parseInt(strs[1]);
                                } else {
                                    b = Double.valueOf(strs[1]).intValue();
                                }
                                int max = Math.max(a, b);
                                int min = Math.min(a, b);
                                mListener.getData(
                                        TimeUtils.getTime(Math.max(a, b)),
                                        TimeUtils.getTime(Math.min(a, b)),
                                        Math.max(a, b),
                                        Math.min(a, b)
                                );
                            }
                        }).start();
                    }
                }
            }
        });
    }


    //修改当前视频进度
    public static void changeMovie(String ip,int progress){
        OkHttpUtil.post("http://" + ip + ":" + mPort + "/?act=unity&states=seek&object=" + progress);
    }


    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }



}
